<template>
  <div v-if="visible" class="modal-overlay" @click.self="closeModal">
    <div class="modal-content" :style="{ width }">
      <!-- Header -->
      <div class="modal-header">
        <span class="modal-title">{{ title }}</span>
        <span class="modal-close" @click="closeModal">×</span>
      </div>

      <!-- Body -->
      <div class="modal-body">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
interface ModalProps {
  title?: string
  width?: string
  visible: boolean
}

const emit = defineEmits(['update:visible'])

withDefaults(defineProps<ModalProps>(), {
  title: '模态框标题',
  width: '450px',
})

const closeModal = () => {
  emit('update:visible', false)
}
</script>

<style scoped>
/* 背景遮罩 */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

/* 模态框主体 */
.modal-content {
  background: #0c1c30;
  /* 深蓝色背景 */
  color: #e0e7ee;
  /* 文字颜色 */
  border-radius: 10px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
  max-height: 75vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  animation: fadeIn 0.3s ease-in-out;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

/* 头部 */
.modal-header {
  padding: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
  background: linear-gradient(90deg, #15406a, #1e5a8a);
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}

/* 标题 */
.modal-title {
  color: #e5f0ff;
}

/* 关闭按钮 */
.modal-close {
  cursor: pointer;
  font-size: 22px;
  color: #e5f0ff;
  transition:
    transform 0.2s,
    color 0.2s;
}

.modal-close:hover {
  color: #ff5a5f;
  transform: rotate(90deg);
}

/* 内容区域（带滚动条） */
.modal-body {
  padding: 15px;
  overflow-y: auto;
  flex: 1;
  max-height: calc(75vh - 50px);
}

/* 美化滚动条 */
.modal-body::-webkit-scrollbar {
  width: 6px;
}

.modal-body::-webkit-scrollbar-track {
  background: #0c1c30;
  border-radius: 3px;
}

.modal-body::-webkit-scrollbar-thumb {
  background: #2f506d;
  border-radius: 3px;
}

.modal-body::-webkit-scrollbar-thumb:hover {
  background: #3f6f9a;
}

/* 模态框淡入动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-15px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
